package org.eoti.math.cf;

import org.eoti.math.Fraction;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class MobiusCF
extends ContinuedFraction
{
	private String fmt;
	private ContinuedFraction x, y;
	private BigInteger a, b, c, d, e, f, g, h;

	public MobiusCF(String fmt, ContinuedFraction x, ContinuedFraction y, BigInteger a, BigInteger b, BigInteger c, BigInteger d, BigInteger e, BigInteger f, BigInteger g, BigInteger h)
	{
		this.fmt = fmt;
		this.x = x;
		this.y = y;
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;
		this.f = f;
		this.g = g;
		this.h = h;
	}

	protected Fraction calculate(BigInteger xn, BigInteger yn)
	{
		if(xn == null) xn = BigInteger.ZERO;
		if(yn == null) yn = BigInteger.ZERO;

		BigInteger A = a.multiply(xn).multiply(yn);
		BigInteger B = b.multiply(xn);
		BigInteger C = c.multiply(yn);
		BigInteger D = d;

		BigInteger E = e.multiply(xn).multiply(yn);
		BigInteger F = f.multiply(xn);
		BigInteger G = g.multiply(yn);
		BigInteger H = h;

		return new Fraction(
				(A.add(B).add(C).add(D)),
		    (E.add(F).add(G).add(H))
		);
	}

	public BigInteger getNumerator( BigInteger index ){
		Fraction fraction = calculate(x.getDenominator(index), y.getDenominator(index));
		fraction = fraction.subtract(new Fraction(getDenominator(index), BigInteger.ONE));
		int scale = fraction.getNumerator().toString().length() + fraction.getDenominator().toString().length();
		scale = scale * 2;
		BigDecimal dec = fraction.compute( scale );
		if(dec == null) return null;
		return dec.toBigInteger();
	}

	public BigInteger getDenominator( BigInteger index ){
		Fraction fraction = calculate(x.getDenominator(index), y.getDenominator(index));
		int scale = fraction.getNumerator().toString().length() + fraction.getDenominator().toString().length();
		scale = scale * 2;
		BigDecimal dec = fraction.compute( scale );
		if(dec == null) return null;
		return dec.toBigInteger();
	}

	public String toString()
	{
		return String.format(fmt, x, y);
	}
}
